﻿/*global ActiveXObject, analogue*//*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: true, plusplus: true, regexp: true, undef: true, white: true, indent: 4*/

analogue.msie = (function () {
    var activators = {
        onactive: {
            on: 'mousedown',
            off: 'mouseup'
        },
        onfocus: {
            on: 'focus',
            off: 'blur'
        },
        onhover: {
            on: 'mouseover',
            off: 'mouseout'
        },
        onunknown: {
            on: 'focus',
            off: 'blur'
        }
    }, getPixels = function (em) {
        var div = document.set('div', { 'style': 'visibility: hidden; position: absolute; left: 1em; top: 1em; width: 1em;' }), pixels;
        document.body.appendChild(div);
        pixels = div.offsetWidth;
        document.body.removeChild(div);
        return parseInt(em, 10) * pixels;
    }, getVersion = function (match, alt) {
        if (match) {
            return Math.max(parseFloat(match[1]), alt);
        }
        return alt;
    }, implement = function (element, value, activator) {
        element.addEvent(activator.on, function () {
            this.addClass(value);
        }).addEvent(activator.off, function () {
            this.removeClass(value);
        });
    }, msie = typeof XMLHttpRequest !== 'object' ? 6 : !document.documentMode ? 7 : getVersion(navigator.userAgent.match(/MSIE ([\d|.]*)/), 8), pseudoExp = (function () {
        if (msie === 6) {
            return '(^|\\s)((([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active))|((a|input|textarea)([#.][^ ]+)?:unknown)';
        }
        if (msie === 7) {
            return '(^|\\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(active|focus)';
        }
        return '(^|\\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(active)';
    }()), setDimensions = function (property) {
        if (!property) {
            return;
        }
        var size = property[3] === 'em' ? getPixels(property[2]) : parseInt(property[2], 10);
        if (property[1].toLowerCase() === 'min-height') {
            document.documentElement.style.minHeight = size;
        } else {
            document.documentElement.style[property[1] + 'Width'] = size;
        }
    };
    return {
        parse: function (sheet) {
            var css, element, elements, i, ii, j, pseudo, rule, rules, selected, selector, value;
            if (sheet.imports) {
                for (i = 0, ii = sheet.imports.length; i < ii; i += 1) {
                    this.parse(sheet.imports[i]);
                }
            }
            rules = sheet.rules;
            for (i = 0, ii = rules.length; i < ii; i += 1) {
                rule = rules[i];
                selector = rule.selectorText;
                css = rule.style.cssText;
                if (msie === 6 && (/(^|\\s)body(\\s|$)/i).test(selector)) {
                    setDimensions((/(min)\-width:\s*([\d\.]+)(em|px)/i).exec(css));
                    setDimensions((/(max)\-width:\s*([\d\.]+)(em|px)/i).exec(css));
                    setDimensions((/(min\-height):\s*([\d\.]+)(em|px)/i).exec(css));
                }
                if ((/opacity:\s*([\d]*[\.\d+]*)/i).test(css) && !analogue.detect.opacity) {
                    if (msie > 7) {
                        sheet.addRule(selector, '-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=' + parseFloat(RegExp.$1 * 100) + ')";');
                    }
                    sheet.addRule(selector, 'zoom: 1; filter: alpha(opacity=' + parseFloat(RegExp.$1 * 100) + ');');
                }
                if (new RegExp(pseudoExp, 'i').test(selector)) {
                    pseudo = selector.replace(/[^:]+:([\w]+)[^\n]*/i, 'on$1');
                    selected = selector.replace(/(\.([a-z0-9_\-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);
                    value = (/\.([a-z0-9_\-]*on(hover|active|focus|unknown))/i).exec(selected)[1];
                    elements = document.getElementsBySelector(selector.replace(/:(hover|active|focus|unknown)[^\n]*$/, ''));
                    if (elements.length > 0) {
                        sheet.addRule(selected, css);
                        for (j = 0; (element = elements[j]); j += 1) {
                            implement(element, value, activators[pseudo]);
                        }
                    }
                }
            }
        },
        resize: function () {
            var body = document.body.style,
                height = Math.max(document.documentElement.clientHeight, document.body.clientHeight) || document.body.clientHeight,
                maxWidth = document.documentElement.style.maxWidth,
                minHeight = parseInt(document.documentElement.style.minHeight, 10),
                minWidth = document.documentElement.style.minWidth,
                width = Math.max(document.documentElement.clientWidth, document.body.clientWidth) || document.body.clientWidth;
            if (maxWidth && width > maxWidth) {
                body.width = maxWidth + 'px';
            } else if (minWidth && width < minWidth) {
                body.width = minWidth + 'px';
            }
            if (minHeight && height < minHeight) {
                body.height = minHeight + 'px';
            }
        },
        version: msie
    };
}());

if (analogue.msie.version === 6) {
    document.createElement('abbr');
    document.documentElement.style.maxWidth = null;
    document.documentElement.style.minHeight = null;
    document.documentElement.style.minWidth = null;
    document.execCommand('BackgroundImageCache', false, true);
    this.addEvent('load', analogue.msie.resize).addEvent('resize', analogue.msie.resize);
    this.XMLHttpRequest = function () { 
        var i, ii, msxml = 'Msxml2.XMLHTTP.6.0,Msxml2.XMLHTTP.3.0,Msxml2.XMLHTTP,Microsoft.XMLHTTP'.split(',');
        for (i = 0, ii = msxml.length; i < ii; i += 1) {
            try {
                return new ActiveXObject(msxml[i]);
            } catch (e) {}
        }
        return false;
    };
}

document.addEvent('ready', function () {
    var i, ii, sheets = document.styleSheets;
    for (i = 0, ii = sheets.length; i < ii; i += 1) {
        analogue.msie.parse(sheets[i]);
    }
});

this.addEvent('load', function () {// Outsmart the Google Toolbar...
    var i, field, fields = document.getElementsBySelector('input, select') || [], restore = function () {
        if (this.style.backgroundColor !== '') {
            this.style.backgroundColor = '';
        }
    };
    for (i = 0; (field = fields[i]); i += 1) {
        field.addEvent('propertychange', restore).style.backgroundColor = '';
    }
});